Lumen & Gesso / website · lumengesso.com / app / admin / orders / [id] / admin-cancel.tsx
| 1 | 'use client'; |
| 2 | |
| 3 | import { useState, type FormEvent } from 'react'; |
| 4 | |
| 5 | // Admin-side cancel panel. Sits at the bottom of the admin order page |
| 6 | // as a clearly-marked "danger zone". Two-step: open the panel, then |
| 7 | // confirm with an optional reason note. The server-side route |
| 8 | // (/api/admin/orders/[id]/cancel) handles Stripe refund + customer |
| 9 | // email + status flip. Prodigi cancellation is admin's responsibility |
| 10 | // out of band if a print order exists. |
| 11 | export function AdminCancel({ |
| 12 | orderId, |
| 13 | status, |
| 14 | hasProdigi, |
| 15 | }: { |
| 16 | orderId: string; |
| 17 | status: string; |
| 18 | hasProdigi: boolean; |
| 19 | }) { |
| 20 | const [open, setOpen] = useState(false); |
| 21 | const [submitting, setSubmitting] = useState(false); |
| 22 | const [reason, setReason] = useState(''); |
| 23 | const [error, setError] = useState<string | null>(null); |
| 24 | const [result, setResult] = useState<{ |
| 25 | refundId: string | null; |
| 26 | refundError: string | null; |
| 27 | prodigiWarning: string | null; |
| 28 | } | null>(null); |
| 29 | |
| 30 | // Mirror the server-side rules so the UI doesn't even hint at cancel |
| 31 | // for terminal orders. (Terminal = cancelled or abandoned — nothing |
| 32 | // to refund, nothing to cancel.) |
| 33 | if (status === 'cancelled' || status === 'abandoned') return null; |
| 34 | |
| 35 | const onCancel = async (e: FormEvent) => { |
| 36 | e.preventDefault(); |
| 37 | setSubmitting(true); |
| 38 | setError(null); |
| 39 | // Bound the request so a stalled refund/email never spins the button |
| 40 | // forever — surface a clear message and let the admin re-check instead. |
| 41 | const controller = new AbortController(); |
| 42 | const timer = setTimeout(() => controller.abort(), 45_000); |
| 43 | try { |
| 44 | const res = await fetch(`/api/admin/orders/${orderId}/cancel`, { |
| 45 | method: 'POST', |
| 46 | headers: { 'content-type': 'application/json' }, |
| 47 | body: JSON.stringify({ reason: reason.trim() || undefined }), |
| 48 | signal: controller.signal, |
| 49 | }); |
| 50 | const data = await res.json().catch(() => null) as { |
| 51 | ok?: boolean; |
| 52 | refundId?: string | null; |
| 53 | refundError?: string | null; |
| 54 | prodigiWarning?: string | null; |
| 55 | error?: string; |
| 56 | } | null; |
| 57 | if (!res.ok || !data?.ok) { |
| 58 | throw new Error(data?.error ?? `Cancel failed (${res.status})`); |
| 59 | } |
| 60 | setResult({ |
| 61 | refundId: data.refundId ?? null, |
| 62 | refundError: data.refundError ?? null, |
| 63 | prodigiWarning: data.prodigiWarning ?? null, |
| 64 | }); |
| 65 | setSubmitting(false); |
| 66 | // No router.refresh() here on purpose. The result panel above is a |
| 67 | // self-contained success state, and the order list / detail pages are |
| 68 | // force-dynamic so they re-fetch fresh on the next navigation anyway. |
| 69 | // Calling refresh() invalidated the whole router cache and kicked off a |
| 70 | // slow RSC refetch (force-dynamic + tunnel latency), which left the App |
| 71 | // Router in a pending transition that froze the next cancel's click. |
| 72 | } catch (err) { |
| 73 | setError( |
| 74 | err instanceof DOMException && err.name === 'AbortError' |
| 75 | ? 'This is taking longer than expected — the cancel may still be processing. Refresh the page to check the order status before retrying.' |
| 76 | : err instanceof Error ? err.message : 'Cancel failed.', |
| 77 | ); |
| 78 | setSubmitting(false); |
| 79 | } finally { |
| 80 | clearTimeout(timer); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | // Post-cancel result panel — shows refund + Prodigi warnings. |
| 85 | if (result) { |
| 86 | return ( |
| 87 | <section className="admin-editor__panel admin-editor__panel--wide admin-cancel admin-cancel--done"> |
| 88 | <h2 className="admin-editor__h" style={{ color: 'var(--ink)' }}>Order cancelled</h2> |
| 89 | <ul className="admin-cancel__result"> |
| 90 | <li> |
| 91 | <strong>Stripe refund:</strong>{' '} |
| 92 | {result.refundError |
| 93 | ? <span className="admin-cancel__warn">FAILED — {result.refundError}. Refund manually via Stripe dashboard.</span> |
| 94 | : result.refundId |
| 95 | ? <code>{result.refundId}</code> |
| 96 | : 'No charge on file (nothing to refund).'} |
| 97 | </li> |
| 98 | {result.prodigiWarning && ( |
| 99 | <li> |
| 100 | <strong>Prodigi:</strong>{' '} |
| 101 | <span className="admin-cancel__warn">{result.prodigiWarning}</span> |
| 102 | </li> |
| 103 | )} |
| 104 | <li>Customer has been emailed.</li> |
| 105 | </ul> |
| 106 | </section> |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | if (!open) { |
| 111 | return ( |
| 112 | <section className="admin-editor__panel admin-editor__panel--wide admin-cancel"> |
| 113 | <details> |
| 114 | <summary className="admin-cancel__trigger">Danger zone — cancel this order</summary> |
| 115 | <button |
| 116 | type="button" |
| 117 | className="btn btn--secondary btn--md" |
| 118 | style={{ marginTop: 12 }} |
| 119 | onClick={() => setOpen(true)} |
| 120 | > |
| 121 | Cancel order… |
| 122 | </button> |
| 123 | </details> |
| 124 | </section> |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | return ( |
| 129 | <section className="admin-editor__panel admin-editor__panel--wide admin-cancel admin-cancel--confirm"> |
| 130 | <h2 className="admin-editor__h" style={{ color: 'var(--rose-deep)' }}>Cancel this order?</h2> |
| 131 | <p className="admin-cancel__body"> |
| 132 | This will: <strong>refund the full charge</strong> via Stripe (if a payment exists), |
| 133 | mark the order cancelled, and email the customer. This is reversible |
| 134 | only manually — there's no undo button. |
| 135 | </p> |
| 136 | {hasProdigi && ( |
| 137 | <p className="admin-cancel__body admin-cancel__warn"> |
| 138 | ⚠ A Prodigi print order already exists for this commission. |
| 139 | Cancelling here does NOT cancel the Prodigi order — you must do that |
| 140 | separately via the Prodigi dashboard, or the print will still ship. |
| 141 | </p> |
| 142 | )} |
| 143 | <form onSubmit={onCancel}> |
| 144 | <label className="admin-editor__label"> |
| 145 | Reason (optional — shown to the customer in the cancellation email) |
| 146 | <textarea |
| 147 | value={reason} |
| 148 | onChange={(e) => setReason(e.target.value)} |
| 149 | rows={3} |
| 150 | className="admin-editor__textarea" |
| 151 | placeholder="e.g. We weren't able to capture the likeness from the photos you sent…" |
| 152 | disabled={submitting} |
| 153 | /> |
| 154 | </label> |
| 155 | <div className="admin-cancel__actions"> |
| 156 | <button |
| 157 | type="button" |
| 158 | className="btn btn--secondary btn--md" |
| 159 | onClick={() => { setOpen(false); setError(null); }} |
| 160 | disabled={submitting} |
| 161 | > |
| 162 | Never mind |
| 163 | </button> |
| 164 | <button |
| 165 | type="submit" |
| 166 | className="btn btn--rose btn--md" |
| 167 | disabled={submitting} |
| 168 | > |
| 169 | {submitting ? 'Cancelling + refunding…' : 'Yes, cancel and refund'} |
| 170 | </button> |
| 171 | </div> |
| 172 | {error && <p className="auth__error" style={{ marginTop: 10 }}>{error}</p>} |
| 173 | </form> |
| 174 | </section> |
| 175 | ); |
| 176 | } |